package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    //新增套餐
    @ApiOperation("新增套餐")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增的套餐:{}",setmealDTO);
        //把套餐添加到套餐表和套餐菜品表
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    //分页查询套餐
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐:{}",setmealPageQueryDTO);
        //分页查询套餐
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    //删除套餐
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result deleteSetmeals(@RequestParam List<Long> ids){
        log.info("删除套餐:{}",ids);
        setmealService.deleteSetmeals(ids);
        return Result.success();
    }

    //起售/禁售套餐
    @PostMapping("/status/{status}")
    @ApiOperation("起售/禁售套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("起售/禁售的套餐id:{}",id);
        setmealService.startOrStop(id,status);
        return Result.success();
    }

    //根据id查询套餐
    @GetMapping("{id}")
    @ApiOperation("根据id查询套餐")
    @Cacheable(cacheNames = "setmeal",key = "#id")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        log.info("查询的套餐id:{}",id);
        SetmealVO setmealVO = setmealService.getSetmealByIdWithDish(id);
        return Result.success(setmealVO);
    }

    //修改套餐
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("要修改的套餐:{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }
}
